package swingxrcp.framework.client.swingx;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;

import swingxrcp.framework.client.swingx.resources.Resources;

/**
 * 闪屏界面,提供两种方案:一是直接显示闪屏,二是闪屏渐现
 *
 * @author Fengyiyun
 *
 */
public class JXSplash extends JXWindow {
	private ImageIcon splashImage = null;

	private final XComponent fc = new XComponent();

	private JLabel labInfos = new JLabel();

	/**
	 *
	 * @param splashImage
	 *            启动界面图片
	 * @param isSplashFadeIn
	 *            启动界面渐现
	 */
	public JXSplash(final ImageIcon splashImage, boolean isSplashFadeIn) {
		super();

		setAlwaysOnTop(true);

		if (isSplashFadeIn) {
			this.addWindowListener(new WindowAdapter() {
				public void windowOpened(WindowEvent e) {
					new Thread(new XController()).start();
				}
			});
		}

		this.splashImage = splashImage;

		Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
		setSize(splashImage.getIconWidth(), splashImage.getIconHeight());
		setLocation(screen.width / 2 - (splashImage.getIconWidth() / 2),
				screen.height / 2 - (splashImage.getIconHeight() / 2));

		JLabel labInfos = new JLabel();
		labInfos.setFont(Resources.DEFAULT_FONT);
		labInfos.setPreferredSize(new Dimension(splashImage.getIconWidth(), 22));

		JPanel bodyPanel = null;

		if (isSplashFadeIn) {
			bodyPanel = new JPanel();
			bodyPanel.setLayout(new BorderLayout());
			bodyPanel.add(fc, "Center");
		} else {
			bodyPanel = new JPanel() {

				public void paintComponent(Graphics g) {
					super.paintComponent(g);
					this.paintBG(g);
				}

				public void paintBG(Graphics g) {
					splashImage.paintIcon(this, g,
							(this.getBounds().width - splashImage.getIconWidth()) / 2,
							(this.getBounds().height - splashImage.getIconHeight()) / 2);
				}
			};
			bodyPanel.setLayout(new BorderLayout());
		}

		bodyPanel.add(labInfos, "South");
		setContentPane(bodyPanel);
	}

	/**
	 * 设置闪屏提示信息
	 *
	 * @param infos
	 */
	public void setSplashInfos(String infos) {
		labInfos.setText("  " + infos);
	}

	/**
	 * 启动闪屏
	 *
	 */
	public void startSplash() {
		setVisible(true);
		toFront();
	}

	/**
	 * 关闭闪屏
	 *
	 */
	public void stopSplash() {
		dispose();
	}

	class XController implements Runnable {
		public void run() {
			try {
				Thread.sleep(500); // pause for 0.5 sec first
				for (int i = 0; i <= 50; i++) {
					fc.alpha = ((float) i) / 50;
					Thread.sleep(50);
					fc.repaint();
				}
			} catch (InterruptedException e) {
			}
		}
	}

	/**
	 * 继承JComponent,重绘组件
	 */
	class XComponent extends JComponent {
		float alpha = 0.0f;

		public void paintComponent(Graphics g) {
			Graphics2D g2d = (Graphics2D) g;
			AlphaComposite comp = AlphaComposite.getInstance(
					AlphaComposite.SRC_OVER, alpha);
			g2d.setComposite(comp);
			g2d.drawImage(splashImage.getImage(), 0, 0, this);
		}
	}
}
